package com.develop.skwen.electricgomall.home;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.develop.skwen.basemodule.ui.BaseFragment;
import com.develop.skwen.electricgomall.home.list.HomeAdapter;
import com.develop.skwen.electricgomall.home.model.HomeData;
import com.develop.skwen.electricgomall.util.HomeDataUtil;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

/**
 * Created by skwen
 * on 18-5-29
 */

public class HomeFragment extends BaseFragment<HomeContract.HomePresenter,HomeContract.HomeVu> implements HomeContract.HomePresenter {


    private HomeAdapter mAdapter;

    private List<HomeData> mDataList = new ArrayList<>();

    @Override
    protected Class<? extends HomeContract.HomeVu> getVuClass() {
        return HomeView.class;
    }

    @Override
    protected HomeContract.HomePresenter getPresenter() {
        return this;
    }

    @Override
    public RecyclerView.LayoutManager getLayoutManager() {
        return new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false);
    }

    @Override
    public void onRefresh() {
        getVu().stopRefresh();
    }

    @Override
    protected void onBind() {

        mAdapter = new HomeAdapter(mDataList);

        getVu().setAdapter(mAdapter);


        loadData();

    }

    private void loadData() {

        Observable.just("home")
                .map(new Function<String, List<HomeData>>() {
                    @Override
                    public List<HomeData> apply(String s) throws Exception {
                        return HomeDataUtil.getInstance().getListHomeData();
                    }
                })
                .subscribe(new Consumer<List<HomeData>>() {
                    @Override
                    public void accept(List<HomeData> homeData) throws Exception {

                        if (homeData != null){
                            mDataList.clear();
                            mDataList.addAll(homeData);
                            mAdapter.notifyDataSetChanged();
                        }

                    }
                });





    }

}
